package io.itit.ecp.admin.provide.action.controller.miniprogram;

import com.baomidou.mybatisplus.core.metadata.IPage;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.ConvertStandardCoalCoefficientQueryReq;
import io.itit.ecp.admin.provide.action.resp.CarbonEmissionFactorResp;
import io.itit.ecp.admin.provide.action.resp.ConvertStandardCoalCoefficientResp;
import io.itit.ecp.admin.server.entity.ConvertStandardCoalCoefficientEntity;
import io.itit.ecp.admin.server.entity.EnergyConsumptionTypeEntity;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Objects;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：折标准煤系数控制类
 * @Date：Create by 2023-11-23 16:28:52
 */
@Api(tags = "折标准煤系数")
@RestController
@RequestMapping("/mini/convertStandardCoalCoefficient")
public class MiniConvertStandardCoalCoefficientController {

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    public ResultResp<IPage<ConvertStandardCoalCoefficientResp>> queryPage(@RequestBody @Validated(IQueryPage.class) ConvertStandardCoalCoefficientQueryReq req) {
        return ResultResp.success(buildQW(req).joinPage(PageUtils.getPage(req, ConvertStandardCoalCoefficientResp.class), ConvertStandardCoalCoefficientResp.class));
    }

    /**
     * 组装查询条件
     */
    private static JoinLambdaWrapper<ConvertStandardCoalCoefficientEntity> buildQW(ConvertStandardCoalCoefficientQueryReq req) {
        LocalDate date = null;
        if (StringUtils.isNotEmpty(req.getSolidifyDate())) {
            date = LocalDate.parse(req.getSolidifyDate()).with(TemporalAdjusters.firstDayOfMonth());
        }
        return Joins.of(ConvertStandardCoalCoefficientEntity.class)
                .selectAll()
                .leftJoin(EnergyConsumptionTypeEntity.class, EnergyConsumptionTypeEntity::getId, ConvertStandardCoalCoefficientEntity::getEnergyTypeId)
                .selectAs(EnergyConsumptionTypeEntity::getEnergyName, CarbonEmissionFactorResp::getEnergyTypeName)
                .end()
                .eq(Objects.nonNull(date), ConvertStandardCoalCoefficientEntity::getSolidifyDate, date)
                .eq(StringUtils.isNotEmpty(req.getEnergyTypeId()), ConvertStandardCoalCoefficientEntity::getEnergyTypeId, req.getEnergyTypeId())
                .last(StringUtils.isNotEmpty(req.getSortColumn()), PageUtils.lastOrderBy(req))
                .orderByDesc(StringUtils.isEmpty(req.getSortColumn()), ConvertStandardCoalCoefficientEntity::getSolidifyDate);
    }
}
